package net.e_agri.json;

import java.io.IOException;

import net.e_agri.cache.ImageCacheEntry;
import net.e_agri.wap.HttpMethod;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public abstract class AbstractContentConverter implements ContentConverter {

	public abstract String parseSource(Source source, String url);

	public static String uriRoot = "http://test1.e-agri.net/";

	protected static Objectify objectify = ObjectifyService.begin();
	private static final Log log = LogFactory.getLog(ContentConverter.class);

	protected String getImageCacheUrl(String imageUrl){
		
		ImageCacheEntry imgCache = null;
		String cacheUrl = null;
		
		try {
			cacheUrl = HttpMethod.doGet("http://0.0.0.0:443/imgCache?files=" + imageUrl);
			if(cacheUrl == null || (cacheUrl != null && cacheUrl.length() == 0)){
				imgCache = objectify.query(ImageCacheEntry.class).filter("sourceUrl", imageUrl).get();

				if(imgCache != null){
					cacheUrl = imgCache.cacheUrl;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		cacheUrl = cacheUrl.replace("\n", "").trim();
		log.info("cacheUrl = " + cacheUrl);
		
		return cacheUrl;
	}
}
